package edu.csumb.itcd.teammario;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;


public class EnemyList implements Runnable {

    public static ArrayList<Enemy> enemyArray = new ArrayList<Enemy>();
    private static ArrayList<Enemy> deadEnemy = new ArrayList<Enemy>();
    private static ArrayList<Money> moneyArray = new ArrayList<Money>();
    private static int enemyMax = 1;
    Thread enemyThread;
    private static Enemy newEnemy;
    private static Money newMoney;
    private static boolean enemyRequested = false;
    private Point playerPosition = new Point(0,0);
    private Random random = new Random();

    public EnemyList()
    {
//    	for (int i = 0; i < enemyMax; i++)
//    	{
//    		spawnEnemyPoint();
//    	}
        enemyThread = new Thread(this);
        enemyThread.start();
    }
    public ArrayList <Enemy> getEnemyArray()
    {
        return EnemyList.enemyArray;
        
    }
    public static ArrayList<Money> getMoneyArray() {
		return moneyArray;
	}
	@Override
    public void run() {
        // TODO Auto-generated method stub
        while (true)
        {
        	if (Setup.isPaused() == false && Setup.isLevelEnded() == false && Setup.isGameOver() == false)
        	{
//        	if (enemyArray.size() < enemyMax)
//            {    
//                synchronized (EnemyList.enemyArray)
//                {
//                    spawnEnemyPoint();
//                }
//            }
//    	if (enemyArray.size() == 0)
//        {    
//    		enemyMax++;
//    		
//    		for (int i = 0; i < enemyMax; i++)
//    		{
//	            synchronized (EnemyList.enemyArray)
//	            {
//	                spawnEnemyPoint();
//	            }
//    		}
//        }
        		synchronized (enemyArray)
        		{
        			EnemyFactory.runLevel(enemyArray);
        		}
        		
	            synchronized (EnemyList.enemyArray)
	            {
	                for (Enemy enemy : enemyArray)
	                {
	                    if (enemy.isActive() == true)
	                    {
	                        enemy.moveAi(playerPosition.x, playerPosition.y);
	                        enemy.flocking(enemyArray);
	                        enemy.setCurrentHealth(enemy.poisonReceived.poisonTarget(enemy.getCurrentHealth()));
	                    }
	                    if (enemy.isActive()== false){
	                        deadEnemy.add(enemy);
	                    }
	                    
	                }
	            }
            
    	
	            synchronized (EnemyList.enemyArray)
	            {
	                
	                if (deadEnemy.isEmpty() == false)
	                {
	                    dropEnemies();
	                }
	            }
	            
	            synchronized (moneyArray)
	            {
	            	for (int i = 0; i < moneyArray.size(); i++)
	            	{
	            		if (moneyArray.get(i).isCollected())
		            	{
		            		moneyArray.remove(i);
		            	}
	            	}
	            }
	            
	            if (EnemyFactory.isLevelEnded() && enemyArray.size() == 0 && moneyArray.size() == 0)
	            {
	            	dropEnemies();
	            	moneyArray.clear();
	            	Setup.setLevelEnd(true);
	            }
        	}
            try
            {
                Thread.sleep(30);
            }
            catch(InterruptedException ex)
            {
                
            }
        }
            
    }
    public void updatePlayerPosition(Point playerPosition)
    {
        this.playerPosition = playerPosition;
    }
    public static void addEnemy(Point spawnPoint, int enemyNum){

        if (enemyNum < 2)
        {
            newEnemy = new RangedEnemy(spawnPoint);
        } else if (enemyNum < 5)
        {
        	newEnemy = new RollEnemy(spawnPoint);
        } else
        {
            newEnemy = new Enemy(spawnPoint);
        }
        
        enemyArray.add(newEnemy);
    }
    public static void dropEnemies(){
//        System.out.println(enemyArray.size());
    	for (Enemy enemy : deadEnemy)
        {
            newMoney = new Money(enemy);
            synchronized (moneyArray)
            {
            	moneyArray.add(newMoney);
            }
            enemyArray.remove(enemyArray.indexOf(enemy));
        }
//        System.out.println(enemyArray.size());
        deadEnemy.clear();
    }
    public void spawnEnemyPoint()
    {
        int temp;
        int quadrant;
        Point spawnPoint = new Point(0,0);
            quadrant = random.nextInt(4) + 1;

            
            if (quadrant == 1)
            {//Left
                temp = random.nextInt(35);
                temp *= -1;
                spawnPoint.x = temp;
                
                temp = random.nextInt(600);
                temp += 25;
                spawnPoint.y = temp;
            }
            else if (quadrant == 2)
            {//Top
                temp = random.nextInt(800);
                temp += 1;
                spawnPoint.x = temp;
                
                temp = random.nextInt(35);
                temp *= -1;
                spawnPoint.y = temp;
            }
            else if (quadrant == 3)
            {//Right
                temp = random.nextInt(35);
                temp += 800;
                spawnPoint.x = temp;
                
                temp = random.nextInt(600);
                temp += 25;
                spawnPoint.y = temp;
            }
            else
            {//Bottom
                temp = random.nextInt(800);
                temp += 1;
                spawnPoint.x = temp;
                
                temp = random.nextInt(35);
                temp += 625;
                spawnPoint.y = temp;
            }
            
            addEnemy(spawnPoint, random.nextInt(10));
    }
	public static int getEnemyMax() {
		return enemyMax;
	}
	public void reset()
	{
		enemyArray.clear();
		deadEnemy.clear();
		moneyArray.clear();
	}
}

